<?php
require_once '../vendor/autoload.php';
use MatchEngine\OrderBook;
use MatchEngine\OrderRedis;

//require_once 'src/OrderBook.php';

$orderbook = new OrderBook('127.0.0.1','6379');


//$order = [
//    'type' => 'order',
//    'data' => [
//        'type' => 'limit',
//        'side' => 'ask',
//        'quantity' => 5,
//        'price' => 0.01,
//        'market' => 'BTC/USDT',
//        'user_id' => 1,
//        'order_id' => 100001,
//    ]
//];
//
//echo json_encode($order);
//die;

//$res = $orderbook->emptys(0);
//var_dump($res);
////die;
//$demoOrder1=[
//    'order_id'  => mt_rand(111111,9999999),
//    'user_id'   => 1,
//    'market'    => 'BTC/USDT',
//    'price'     => 0.0001211,
//    'quantity'  => 10,
//    'side'      => 'bid',
//    'type'      => 'limit'
//];
//
//$demoOrder2=[
//    'order_id'  => mt_rand(111111,9999999),
//    'user_id'   => 1,
//    'market'    => 'BTC/USDT',
//    'price'     => 0.0002987,
//    'quantity'  => 9.4,
//    'side'      => 'bid',
//    'type'      => 'limit'
//];
//
//$demoOrder3=[
//    'order_id'  => mt_rand(111111,9999999),
//    'user_id'   => 1,
//    'market'    => 'BTC/USDT',
//    'price'     => 0.0002152,
//    'quantity'  => 3.1,
//    'side'      => 'bid',
//    'type'      => 'limit'
//];
//
//
//$demoOrder4=[
//    'order_id'  => mt_rand(111111,9999999),
//    'user_id'   => 1,
//    'market'    => 'BTC/USDT',
//    'price'     => 0.0004123,
//    'quantity'  => 1.1,
//    'side'      => 'ask',
//    'type'      => 'limit'
//];
//
//$demoOrder5=[
//    'order_id'  => mt_rand(111111,9999999),
//    'user_id'   => 1,
//    'market'    => 'BTC/USDT',
//    'price'     => 0.00124,
//    'quantity'  => 3.5,
//    'side'      => 'ask',
//    'type'      => 'limit'
//];
//$demoOrder6=[
//    'order_id'  => mt_rand(111111,9999999),
//    'user_id'   => 1,
//    'market'    => 'BTC/USDT',
//    'price'     => 0,
//    'quantity'  => 10,
//    'side'      => 'ask',
//    'type'      => 'market'
//];
// $res=$orderbook->processOrder($demoOrder1);
// $res=$orderbook->processOrder($demoOrder2);
// $res=$orderbook->processOrder($demoOrder3);
// $res=$orderbook->processOrder($demoOrder4);
// $res=$orderbook->processOrder($demoOrder5);
// $res=$orderbook->processOrder($demoOrder6);

//
//$res=$orderbook->getHandicap('BTC/USDT');
//$depth_bids = do_foreach($res['bids'], 6, 'bids');
//$depth_asks = do_foreach($res['asks'], 6, 'asks');
//for ($i = 0; $i <= 5; $i++) {
//    $asks = $depth_asks['step' . $i];
//    $bids = $depth_bids['step' . $i];
//    if (end($asks)['price'] == $bids[0]['price']) {
//        $bids[0]['price'] = 0;
//    }
//    $push_data = json_encode([
//        'timestamp' => time(),
//        'asks' => $asks,
//        'bids' => $bids,
//    ]);
//    echo $i.'===========',PHP_EOL;
//    var_dump($push_data);
//}


//var_dump(json_encode($res['asks']));
//var_dump(json_encode($depth_bids));
//var_dump($depth_asks);
//die;

//$orderRedis = new OrderRedis('127.0.0.1', 6379);
//$a = $orderRedis->getPriceArea('BTC/USDT', 'limit', 'bid', 13*1e8, 16*1e8);
//var_dump($a);
//die;

$btime = microtime(true);
for ($i=1;$i<=500;$i++){
    $type = ['ask','bid'];
    $demoOrder6=[
        'order_id'  => mt_rand(11111111,999999999),
        'user_id'   => 1,
        'market'    => 'BTC/USDT',
        'price'     => mt_rand(10000,40000)/1000000,
//        'price'     => 0,
//        'quantity'  => mt_rand(10000,100000),
        'quantity'  => 10,
        'side'      => $type[1],
        'type'      => 'limit'
    ];
//    var_dump($demoOrder6);
    $res=$orderbook->processOrder($demoOrder6);
//    var_dump(count($res['matchArr']));
}
for ($i=1;$i<=500;$i++){
    $type = ['ask','bid'];
    $demoOrder6=[
        'order_id'  => mt_rand(11111111,999999999),
        'user_id'   => 1,
        'market'    => 'BTC/USDT',
        'price'     => mt_rand(10000,40000)/1000000,
//        'price'     => 0,
//        'quantity'  => mt_rand(10000,100000),
        'quantity'  => 10,
        'side'      => $type[0],
        'type'      => 'limit'
    ];
//    var_dump($demoOrder6);
    $res=$orderbook->processOrder($demoOrder6);
//    var_dump(count($res['matchArr']));
}
$etime = microtime(true);
echo $etime - $btime,PHP_EOL;



/**
 * 遍历整合数据
 * @param array $data 买卖盘数据
 * @param int $decimal 精度
 * @param string $type 买卖盘方向
 * @return array
 */
function do_foreach(array $data, int $decimal, string $type)
{
    $step0 = $step1 = $step2 = $step3 = $step4 = $step5 = [];
    foreach ($data as $val) {
        $step0 = dmStep($step0, dmPride($val['price'], 0, $decimal, $type), $val['totalSize']);
        $step1 = dmStep($step1, dmPride($val['price'], 1, $decimal, $type), $val['totalSize']);
        $step2 = dmStep($step2, dmPride($val['price'], 2, $decimal, $type), $val['totalSize']);
        $step3 = dmStep($step3, dmPride($val['price'], 3, $decimal, $type), $val['totalSize']);
        $step4 = dmStep($step4, dmPride($val['price'], 4, $decimal, $type), $val['totalSize']);
        $step5 = dmStep($step5, dmPride($val['price'], 5, $decimal, $type), $val['totalSize']);
    }
    return [
        'step0' => $step0,
        'step1' => $step1,
        'step2' => $step2,
        'step3' => $step3,
        'step4' => $step4,
        'step5' => $step5,
    ];
}

/**
 * 价格精度保留
 * @param float $price 原始值
 * @param int $x 深度0 1 2 3 4 5
 * @param int $decimal 币种保留小数位
 * @param string $type
 * @return string
 */
function dmPride(float $price, int $x, int $decimal, string $type)
{
    if ($x == 0) {
        return number_format($price, $decimal, '.', '');
    }
    $raw_price = number_format($price, $decimal, '.', '');
    $int_price = $raw_price * pow(10, $decimal);

    $a = number_format($int_price * pow(10, -$x), $decimal, '.', '');
    $arr = explode('.', $a);
    list($l, $r) = $arr;
    //卖盘向上取， 买盘向下取
    if ($type == 'asks' && $r > 0) {
        $l += 1;
    }
    return number_format($l * pow(10, $x) * pow(10, -$decimal), $decimal, '.', '');
}

/**
 * 合并数量
 * @param array $step 待整合数组
 * @param float $price 价格
 * @param float $totalSize 数量
 * @return array
 */
function dmStep(array $step, float $price, float $totalSize)
{
    $tmp = [
        'price' => $price,
        'totalSize' => $totalSize
    ];
    $count = count($step);
    if ($count > 0 && $step[$count - 1]['price'] == $price) {
        $step[$count - 1]['totalSize'] += $totalSize;
    } else {
        array_push($step, $tmp);
    }
    return $step;
}
